﻿using SHZY.Common.Core.Manager;
using SHZY.Common.Core.Security;
using SHZY.Common.Enum;
using SHZY.Common.Filter;
using SHZY.DependencyInjection;
using SHZY.DynamicApiController;
using SHZY.FriendlyException;
using SHZY.Systems.Interfaces.System;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using SHZY.BaseData.Entitys.Common.Entity;
using System.Linq;
using SHZY.Business.Interfaces.Common;
using SHZY.Business.Entity.Common;

namespace SHZY.Business.Common;

/// <summary>
/// 业务实现：打印配置选择.
/// </summary>
[ApiDescriptionSettings(Tag = "Business", Name = "PrintConfigSelect", Order = 200)]
[Route("api/Common/Business/[controller]")]
public class ComBasePrintConfigSelectService : IComBasePrintConfigSelectService, IDynamicApiController, ITransient
{
    /// <summary>
    /// 多租户事务.
    /// </summary>
    private readonly ITenant _db;

    /// <summary>
    /// 服务基础仓储.
    /// </summary>
    private readonly ISqlSugarRepository<ComBaseEventEntity> _repository;

    /// <summary>
    /// 数据接口服务.
    /// </summary>
    private readonly IDataInterfaceService _dataInterfaceService;

    /// <summary>
    /// 用户管理.
    /// </summary>
    private readonly IUserManager _userManager;

    /// <summary>
    /// 初始化一个<see cref="ComBasePrintConfigSelectService"/>类型的新实例.
    /// </summary>
    public ComBasePrintConfigSelectService(
        ISqlSugarRepository<ComBaseEventEntity> comBasePrintConfigRepository,
        IDataInterfaceService dataInterfaceService,
        ISqlSugarClient context,
        IUserManager userManager)
    {
        _repository = comBasePrintConfigRepository;
        _dataInterfaceService = dataInterfaceService;
        _userManager = userManager;
        _db = context.AsTenant();
    }

    /// <summary>
    /// 查询是否有默认打印配置.
    /// 秦静
    /// 2023年11月12日
    /// </summary>
    /// <param name="EventCode">打印事件编号.</param>
    /// <returns></returns>
    [HttpGet("GetDefaultPrintConfig")]
    public async Task<dynamic> GetDefaultPrintConfig(string EventCode)
    {
        var data = await _repository.FirstOrDefaultAsync(x => x.EventCode == EventCode && x.DeleteMark == null);
        if (data != null)
        {
            return data?.PrintConfigId;
        }
        return "";
    }

    /// <summary>
    /// 设置默认打印配置
    /// 秦静
    /// 2023年11月12日
    /// </summary>
    /// <param name="EventCode">打印事件编号（每个打印模块的唯一标识）</param>
    /// <param name="PrintConfigId">打印配置的Id</param>
    /// <returns></returns>
    [HttpGet("SetDefaultPrintConfig")]
    public async Task SetDefaultPrintConfig(string EventCode, string PrintConfigId)
    {
        var data = await _repository.FirstOrDefaultAsync(x => x.EventCode == EventCode && x.DeleteMark == null);

        var isOk = 0;
        if (data != null && data?.PrintConfigId != PrintConfigId)
        {
            isOk = await _repository.Context.Updateable<ComBaseEventEntity>().Where(x => x.EventCode == EventCode && x.DeleteMark == null).SetColumns(x => new ComBaseEventEntity()
            {
                PrintConfigId = PrintConfigId,
                LastModifyTime = DateTime.Now,
                LastModifyUserId = _userManager.UserId
            }).ExecuteCommandAsync();
        }
        else
        {
            ComBaseEventEntity model = new ComBaseEventEntity()
            {
                EventCode = EventCode,
                PrintConfigId = PrintConfigId,
            };
            isOk = await _repository.Context.Insertable(model).CallEntityMethod(x => x.Create()).ExecuteCommandAsync();

        }
        if (isOk == 0)
        {
            throw Oops.Oh(ErrorCode.COM1008);
        }
    }

}